package oh.june.calleridentifier;

import oh.june.calleridentifier.module.DataRow;
import oh.june.calleridentifier.module.DataRowBean;
import oh.june.calleridentifier.module.DbHelper;
import oh.june.calleridentifier.module.RoundButton;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class MemoActivity extends Activity {
	
	DbHelper _helper = DbHelper.getInstance(MemoActivity.this);
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		DataRow row = new DataRow();
		
		if(getIntent().hasExtra("MODIFY_DATA")){
			DataRowBean bean = (DataRowBean) getIntent().getSerializableExtra("MODIFY_DATA");
			row = bean.getDataRow();
		}else if(getIntent().hasExtra("NUMBER")){
			String number = getIntent().getStringExtra("NUMBER");
			if(number == null)
				finish();
			row = _helper.selectMainRow(number);
			row.put("NUMBER", number);
		}
		// Other case can register new number & memo by user 
		
		final DataRow rowItem = row;
		final EditText edt_number = (EditText)findViewById(R.id.edt_number);
		edt_number.setText(row.getString("NUMBER", ""));
		final EditText edt_name = (EditText)findViewById(R.id.edt_name);
		final EditText edt_memo = (EditText)findViewById(R.id.edt_memo);
		RoundButton btnSave = (RoundButton)findViewById(R.id.btn_save);
		btnSave.setStyle(RoundButton.TYPE_RED);
		
		edt_name.setText(row.getString("NAME", ""));
		edt_memo.setText(row.getString("MEMO", ""));
		
		btnSave.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String num = edt_number.getText().toString();
				String nam = edt_name.getText().toString();
				String mem = edt_memo.getText().toString();
				
				if(num.equals("") || nam.equals("")){
					String toastMessage = getResources().getString(R.string.memoactivity_check_validation);
					Toast.makeText(MemoActivity.this, toastMessage, Toast.LENGTH_SHORT).show();
					if(num.equals(""))
						edt_number.requestFocus();
					else
						edt_name.requestFocus();
					return;
				}
				
				try {
					if(!_helper.isInMyDb(num))
						_helper.insertMainTable(num, nam);	
					else
						_helper.updateMainTable(num, nam);
					
					String insDt = rowItem.getString("INS_DT", "");
					
					if(!_helper.isInMyDb2(rowItem)){
						_helper.insertMemoTable(num, nam, mem);
					}else{
						_helper.updateMemoTable(num, nam, insDt, mem);
					}
					
					
				} catch (Exception e) {
					e.printStackTrace();
				} finally{
					setResult(Activity.RESULT_OK);
					MemoActivity.this.finish();
				}
			}
		});
		
		RoundButton btnClose = (RoundButton)findViewById(R.id.btn_close);
		btnClose.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
}
